package sunwin.yoms.web.controller.person;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import sunwin.yoms.comm.YomsException;
import sunwin.yoms.comm.YomsFunction;
import sunwin.yoms.domain.person.UserRoleDomain;
import sunwin.yoms.domain.system.SysFuncMapDomain;
import sunwin.yoms.domain.system.SysFunctionDomain;
import sunwin.yoms.service.person.RoleService;
import sunwin.yoms.service.system.SysFunctionService;
import sunwin.yoms.web.comm.R;
import sunwin.yoms.web.comm.YomsAjaxAnswer;
import sunwin.yoms.web.comm.YomsController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by mt on 2015-8-11.
 */
@Controller
public class PermissionController {
    @Autowired
    private SysFunctionService functionService;
    @Autowired
    private RoleService roleService;

    /**
     * 访问分配权限页面
     */
    @RequestMapping(value = R.AllotRoleToPermission, method = RequestMethod.GET)
    @YomsController(functionNo = YomsFunction.AllotPermission, viewName = "person/permission.allot")
    public ModelAndView allotPermission(@RequestParam(required = false, defaultValue = "0") int rid) throws YomsException {
        ModelAndView mav = initData();

        if (rid > 0) {
            UserRoleDomain role = roleService.getRoleById(rid);
            Map<Integer, Boolean> map = functionService.getRoleFunc(role);
            mav.addObject("funcs", map);
            mav.addObject("rid", rid);
        }

        return mav;
    }

    @RequestMapping(value = R.AllotRoleToPermission, method = RequestMethod.POST)
    @YomsController(functionNo = YomsFunction.AllotPermission)
    @ResponseBody
    public YomsAjaxAnswer allotPermission(int rid, int fid, boolean isAllot) throws YomsException {
        functionService.setRoleFunc(rid, fid, isAllot);

        YomsAjaxAnswer answer = new YomsAjaxAnswer();
        answer.setSuccess(true);
        return answer;
    }

    private ModelAndView initData() throws YomsException {
        List<SysFunctionDomain> personFns = functionService.getPersonFunction();
        List<SysFunctionDomain> publictyFns = functionService.getPublictyFunction();
        List<SysFunctionDomain> facilityFns = functionService.getFacilityFunction();
        List<SysFunctionDomain> settingFns = functionService.getSettingFunction();
        List<UserRoleDomain> roles = roleService.getRoles();

        ModelAndView mav = new ModelAndView();
        mav.addObject("roles", roles);
        mav.addObject("personFns", personFns);
        mav.addObject("publictyFns", publictyFns);
        mav.addObject("facilityFns", facilityFns);
        mav.addObject("settingFns", settingFns);
        return mav;
    }
}
